package adb;

import static adb.ADBView.command;
import static adb.ADBView.jProgressBarDoInBackground;
import static adb.ADBView.jTextAreaStatus;
import static adb.ADBView.output;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.SwingWorker;

public class runCommand extends SwingWorker<Void, String> {

    @Override
    protected Void doInBackground() throws Exception {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        int progress=0;
        jProgressBarDoInBackground.setValue(progress);
        String[] cc = command.split(" ");
        if (command.charAt(0) != '0'&&command.length()>1) {
            try {
                String line = "";
                ProcessBuilder pb = new ProcessBuilder(cc);
                pb.redirectErrorStream(true);
                Process process = pb.start();
                BufferedReader inStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                
                progress=10;
                jProgressBarDoInBackground.setValue(progress);
                while ((line = inStreamReader.readLine()) != null) {
                    
                    if (line.length() > 0) {
                        //output= output+ line+"\n";
                        //out = out + line + "\n";
                        
                        output=output+line+"\n";
                        jTextAreaStatus.setText(output);
                        progress=progress+10;
                        jProgressBarDoInBackground.setValue(progress);
                    }
                }
            } catch (IOException ex) {
                output = output + ex + "\n";
                jTextAreaStatus.setText(output);
            }
        } else {
            //System.out.print(command.charAt(0));
            output=output + "Command is empty.\n";
            jTextAreaStatus.setText(output);
        }
        jProgressBarDoInBackground.setValue(0);
        return null;
    }
    
    
    @Override
        protected void done() {
           jProgressBarDoInBackground.setValue(0);
        }
}
